/**
 * @author: Niimp
 * @date: 2023/11/28 9:37
 * @statement:
 */
package com.dmgis.hndz.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aspose.slides.Collections.Specialized.CollectionsUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dmgis.hndz.service.DynamicWarningService;
import com.dmgis.hndz.common.WebCtrlData;
import com.dmgis.hndz.common.config.DbConfig2;
import com.dmgis.hndz.common.exception.BusinessException;
import com.dmgis.hndz.common.UrlHttpRequest;
import com.dmgis.hndz.common.utils.StringUtil;
import com.dmgis.hndz.common.utils.TimeUtil;
import com.dmgis.hndz.mapper.DynamicWarningMapper;
import com.dmgis.hndz.pojo.*;
import com.dmgis.hndz.pojoVo.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static com.dmgis.hndz.common.utils.LongitudeAndLatitudeUtil.stringToLongitudeAndLatitude;

@Service
public class DynamicWarningServiceImpl implements DynamicWarningService {

    @Resource
    private DynamicWarningMapper dynamicWarningMapper;


    /***
     * 查询天气预报  未来三天
     * @param code
     * @param name
     * @return
     */
    @Override
    public List<Qxba01a> queryThreeCityForecast(String code, String name) throws BusinessException {
        List<Qxba01a> list = new ArrayList<>();
        try {
            UrlHttpRequest request = new UrlHttpRequest();
            String param = "";
            if (code.endsWith("0000"))
                param = " qxba01a170 like '%长沙市%' and qxba01a050 in ('12','24','36','48','60','72') and qxba01a030 = (select qxba01a030 from qxba01a where qxba01a170 like '%长沙市%' order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a040 = (select qxba01a040 from qxba01a order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a020 = (select DISTINCT(qxba01a020) from qxba01a where qxba01a170 like '%长沙市%' limit 1) order by qxba01a020,qxba01a050";
            else if (code.endsWith("00"))
                param = " qxba01a170 like '%" + name + "%' and qxba01a050 in ('12','24','36','48','60','72') and qxba01a030 = (select qxba01a030 from qxba01a where qxba01a170 like '%" + name + "%' order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a040 = (select qxba01a040 from qxba01a order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a020 = (select DISTINCT(qxba01a020) from qxba01a where qxba01a170 like '%" + name + "%' limit 1) order by qxba01a020,qxba01a050";
            else
                param = " qxba01a180 like '%" + name.replace("市", "").replace("县", "") + "%' and qxba01a050 in ('12','24','36','48','60','72') and qxba01a030 = (select qxba01a030 from qxba01a where qxba01a180 like '%" + name.replace("市", "").replace("县", "") + "%' order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a040 = (select qxba01a040 from qxba01a order by qxba01a030 desc,qxba01a040 limit 1) and qxba01a020 = (select DISTINCT(qxba01a020) from qxba01a where qxba01a180 like '%" + name.replace("市", "").replace("县", "") + "%' limit 1) order by qxba01a020,qxba01a050";
            String url = DbConfig2.Uri + "QX/SelectQxba01A";
            HashMap<String, Object> map = new HashMap<>();
            map.put("param", param);
            String result = request.httpPost1(url, map);
            if (!StringUtil.IsNullOrEmpty(result)) {
                JSONObject object = JSONObject.parseObject(result);
                JSONArray array = object.getJSONArray("data");
                list = JSONObject.parseArray(array.toJSONString(), Qxba01a.class);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new BusinessException("方法名：DynamicWarningService.queryThreeCityForecast,"
                    + e.getMessage());
        }
        return list;
    }

    //这里改成hndz_qxdata库
    @DS("slave_qxdata")
    @Override
    public List<LocationAndCode> queryLocationAndCode() {
        List<LocationAndCode> list = dynamicWarningMapper.queryLocationAndCode();
        return list;
    }

    @DS("slave_qxdata")
    @Override
    public List<ThreeForecastWaterVo> queryThreeDayWeatherForecast2(JSONObject jsonObject) {

        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }

        String cityName = jsonObject.getString("cityName");
        String countyName = jsonObject.getString("countyName");
        String todayTime = jsonObject.getString("todayTime");//08-30
        String sc = jsonObject.getString("sc");//08
        String ntimes = jsonObject.getString("ntimes");//24
        //查询三天天气预报预报数据
        List<Qxba01AVo> list = dynamicWarningMapper.queryThreeDayWeatherForecast2(cityName, countyName, todayTime, sc, ntimes);
        //数据处理
        List<ThreeForecastWaterVo> threeForecastWaterVos = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            list.forEach(l -> {
                if (l.getQxba01a060() != null && !"".equals(l.getQxba01a060())
                        && Integer.valueOf(l.getQxba01a060()) < -100) {
                    l.setQxba01a060("0");
                }
                if (l.getQxba01a070() != null && !"".equals(l.getQxba01a070())
                        && Integer.valueOf(l.getQxba01a070()) < -100) {
                    l.setQxba01a070("0");
                }
            });
            if (list.size() == 1) {
                list.add(list.get(0));
            }
        }
        for (int i = 0; i < list.size() / 2; i++) {
            ThreeForecastWaterVo threeForecastWaterVo = new ThreeForecastWaterVo();
            String qxba01a030 = list.get(2 * i + 1).getQxba01a030();
            String time = list.get(2 * i + 1).getQxba01a050();
            String date = qxba01a030.substring(0, 10);   //年-月-日
            String format = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = sdf.parse(date);
                format = sdf.format(parse);
                threeForecastWaterVo.setStartTime(sdf.format(parse));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            //转星期
            String week = TimeUtil.getWeekStr(format);
            threeForecastWaterVo.setDateTime(week);
            String fx12 = WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a100());
            threeForecastWaterVo.setTwentyFourWindDirection(fx12);
            String fx24 = WebCtrlData.GetWeatherFx(list.get(2 * i).getQxba01a100());
            threeForecastWaterVo.setTwelveWindDirection(fx24);
            threeForecastWaterVo.setWindDirection(fx12 + "~" + fx24);
            String fs12 = WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a080());
            threeForecastWaterVo.setTwentyFourWindPower(fs12);
            String fs24 = WebCtrlData.GetWeatherFs(list.get(2 * i).getQxba01a080());
            threeForecastWaterVo.setTwelveWindSpeed(fs24);
            threeForecastWaterVo.setStartingTime(list.get(2 * i + 1).getQxba01a040());
            threeForecastWaterVo.setWindForce(fs12 + "~" + fs24);

//            if (Objects.equals(list.get(2 * i + 1).getQxba01a060(), "-65535")) {
            if (Objects.equals(list.get(2 * i + 1).getQxba01a060(), "0")) {
                threeForecastWaterVo.setMaximumTemperature(list.get(2 * i).getQxba01a060());
            } else {
                threeForecastWaterVo.setMaximumTemperature(list.get(2 * i + 1).getQxba01a060());
            }
//            if (Objects.equals(list.get(2 * i + 1).getQxba01a070(), "-65535")) {
            if (Objects.equals(list.get(2 * i + 1).getQxba01a070(), "0")) {
                threeForecastWaterVo.setMinimumTemperature(list.get(2 * i).getQxba01a070());
            } else {
                threeForecastWaterVo.setMinimumTemperature(list.get(2 * i + 1).getQxba01a070());
            }
            threeForecastWaterVo.setLongitude(stringToLongitudeAndLatitude(list.get(2 * i + 1).getQxba01a140()));
            threeForecastWaterVo.setLatitude(stringToLongitudeAndLatitude(list.get(2 * i + 1).getQxba01a150()));
            String qxba01a120 = list.get(2 * i).getQxba01a120();
            if (qxba01a120.length() < 2)
                qxba01a120 = "0" + qxba01a120;
            else qxba01a120 = "00";
            threeForecastWaterVo.setWeatherPhenomenon(qxba01a120);
            String qxba01a130 = list.get(2 * i + 1).getQxba01a130();
            if (qxba01a130.length() < 2)
                qxba01a130 = "0" + qxba01a130;
            else qxba01a130 = "00";
            threeForecastWaterVo.setTwentyFourWeatherPhenomenon(qxba01a130);
            String wea12 = WebCtrlData.GetWeatherTq(qxba01a120);
            String wea24 = WebCtrlData.GetWeatherTq(qxba01a130);
            if (qxba01a120.equals(qxba01a130)) {
                threeForecastWaterVo.setWeather(wea12);
            } else {
                threeForecastWaterVo.setWeather(wea12 + "转" + wea24);
            }
            threeForecastWaterVo.setCity(list.get(2 * i + 1).getQxba01a170());
            threeForecastWaterVo.setCounty(list.get(2 * i + 1).getQxba01a180());
            threeForecastWaterVos.add(threeForecastWaterVo);
        }
        // 城镇预报记录去除
        List<ThreeForecastWaterVo> threeForecastWaterList = threeForecastWaterVos.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCity()
                        + "-" + o.getCounty()
                        + "-" + o.getWindDirection()
                        + "-" + o.getTwentyFourWindPower()
                        + "-" + o.getMaximumTemperature()
                        + "-" + o.getMinimumTemperature()
                        + "-" + o.getWeather()
                ))), ArrayList::new
        ));
        return threeForecastWaterList;

    }


    @DS("slave_qxdata")
    @Override
    public List<Ybba01D> queryYbba01D(String hourtype) {
        List<Ybba01D> list = dynamicWarningMapper.queryYbba01D(hourtype);
        return list;
    }


    /**
     * 最新监控
     *
     * @param jsonObject
     * @return
     */
    @DS("slave_qxdata")
    @Override
    public List<LatestMonitoring> queryLatestMonitoring(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String time = jsonObject.getString("time");
        List<LatestMonitoring> list = dynamicWarningMapper.queryLatestMonitoring(time);
        return list;
    }


    /**
     * 最新监控详细信息
     *
     * @param jsonObject
     * @return
     */
    @DS("slave_qxdata")
    @Override
    public List<LatestMonitoring> queryLatestMonitoringInfo(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String jkba01a100 = jsonObject.getString("time");
        String jkba01a050 = jsonObject.getString("county");
        String jkba01a080 = jsonObject.getString("waringLevel");
        String endTime = jsonObject.getString("endTime");
        List<LatestMonitoring> list = dynamicWarningMapper.queryLatestMonitoringInfo(jkba01a100, jkba01a050, jkba01a080, endTime);
        return list;
    }

    /**
     * 预警发布统计
     *
     * @param jsonObject
     * @return
     */
    @DS("slave_qxdata")
    @Override
    public Map<String, Integer> queryWarningReleaseStatistics(JSONObject jsonObject) {

        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String monthYear = jsonObject.getString("monthYear");
        String time = jsonObject.getString("time");
        String ybba01b020 = jsonObject.getString("warningType");
        List<WaringType> list;
        Map<String, Integer> countMap;
        if ("1".equals(monthYear)) { // 年
            list = dynamicWarningMapper.queryWarningReleaseStatisticsYear(time, ybba01b020);
            // 统计各个预警等级的数量
            countMap = list.stream()
                    .collect(Collectors.groupingBy(WaringType::getWaringLevel, Collectors.summingInt(e -> 1)));

            // 计算总的预警等级的数量
            int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();

            // 将总计数量存入Map
            countMap.put("总计", totalCount);
        } else {
            list = dynamicWarningMapper.queryWarningReleaseStatisticsMonth(time, ybba01b020);
            countMap = list.stream()
                    .collect(Collectors.groupingBy(WaringType::getWaringLevel, Collectors.summingInt(e -> 1)));
            // 计算总的预警等级的数量
            int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();

            // 将总计数量存入Map
            countMap.put("总计", totalCount);
        }
        return countMap;
    }

    @DS("slave_qxdata")
    @Override
    public Map<String, Integer> queryWaringStatisticsAnalysis(String startTime, String endTime) {
        Map<String, Integer> countMap = new HashMap<>();
        List<WaringStatistics> waringStatistics = new ArrayList<>();
        waringStatistics = dynamicWarningMapper.queryWaringStatisticsAnalysis(startTime, endTime);
        countMap = waringStatistics.stream()
                .collect(Collectors.groupingBy(WaringStatistics::getWaringLevel, Collectors.summingInt(e -> 1)));
        //计算统计的个数
        int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();
        // 将总计数量存入Map
        countMap.put("总计", totalCount);
        return countMap;
    }

    @DS("slave_qxdata")
    @Override
    public List<ThreeForecastWaterVo> queryThreeDayWeatherForecast(JSONObject jsonObject) {

        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }

        String cityName = jsonObject.getString("cityName");
        String countyName = jsonObject.getString("countyName");
        String todayTime = jsonObject.getString("todayTime");
        //前端传入一个areaCode
        String areaCode = jsonObject.getString("areaCode");
        //查询三天天气预报预报数据
        List<Qxba01AVo> list = dynamicWarningMapper.queryThreeDayWeatherForecast(cityName, countyName, todayTime, areaCode);

        //数据处理
        List<ThreeForecastWaterVo> threeForecastWaterVos = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < list.size() / 2; i++) {
            ArrayList qxaa01AS = new ArrayList<>();
            ThreeForecastWaterVo threeForecastWaterVo = new ThreeForecastWaterVo();
//            String qxba01a030 = list.get(i).getQxba01a030();
            String qxba01a030 = list.get(2 * i + 1).getQxba01a030();
            String time = list.get(2 * i + 1).getQxba01a050();
//            String time = list.get(i).getQxba01a050();
            String date = qxba01a030.substring(0, 10);   //年-月-日
            String format = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = sdf.parse(date);
                format = sdf.format(parse);
//                map.put("起报日期", sdf.format(parse));
                threeForecastWaterVo.setStartTime(sdf.format(parse));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            //转星期
            String week = TimeUtil.getWeekStr(format);
//            map.put("日期", week);
            threeForecastWaterVo.setDateTime(week);
            //风速
            String fx = WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a100());
//            map.put("风向", fx);
            threeForecastWaterVo.setWindDirection(fx);
            String fs = WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a080());
//            map.put("风力", fs);
            threeForecastWaterVo.setWindForce(fs);
//            map.put("起报时次", list.get(2 * i + 1).getQxba01a040());
            threeForecastWaterVo.setStartingTime(list.get(2 * i + 1).getQxba01a040());
//            map.put("时效", time);
            threeForecastWaterVo.setTimeliness(time);
//            map.put("最高气温", list.get(2 * i + 1).getQxba01a060());
            threeForecastWaterVo.setMaximumTemperature(list.get(2 * i + 1).getQxba01a060());
//            map.put("最低气温", list.get(2 * i + 1).getQxba01a070());
            threeForecastWaterVo.setMinimumTemperature(list.get(2 * i + 1).getQxba01a070());
//            map.put("12小时风力", WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a080()));
            threeForecastWaterVo.setTwelveWindSpeed(WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a080()));
//            map.put("24小时风力", WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a090()));
            threeForecastWaterVo.setTwentyFourWindPower(WebCtrlData.GetWeatherFs(list.get(2 * i + 1).getQxba01a090()));
//            map.put("12小时风向", WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a100()));
            threeForecastWaterVo.setTwelveWindDirection(WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a100()));
//            map.put("24小时风向", WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a110()));
            threeForecastWaterVo.setTwentyFourWindDirection(WebCtrlData.GetWeatherFx(list.get(2 * i + 1).getQxba01a110()));
            String qxba01a120 = list.get(2 * i).getQxba01a120();
            if (qxba01a120.length() < 2)
                qxba01a120 = "0" + qxba01a120;
            else qxba01a120 = "00";
//            map.put("天气现象", qxba01a120);
            threeForecastWaterVo.setWeatherPhenomenon(qxba01a120);
            qxba01a120 = list.get(2 * i + 1).getQxba01a120();
            if (qxba01a120.length() < 2)
                qxba01a120 = "0" + qxba01a120;
            else qxba01a120 = "00";
//            map.put("24天气现象", qxba01a120);
            threeForecastWaterVo.setTwentyFourWeatherPhenomenon(qxba01a120);
//            map.put("所属市", list.get(2 * i + 1).getQxba01a170());
            threeForecastWaterVo.setCity(list.get(2 * i + 1).getQxba01a170());
//            map.put("所属县", list.get(2 * i + 1).getQxba01a180());
            threeForecastWaterVo.setCounty(list.get(2 * i + 1).getQxba01a180());
//            qxaa01AS.add(map);
//            pList.add(qxaa01AS);
            threeForecastWaterVos.add(threeForecastWaterVo);
        }
        return threeForecastWaterVos;
    }

    /**
     * 预报监控
     *
     * @param jsonObject
     * @return
     */
    @DS("slave_qxdata")
    @Override
    public List<String> queryforecastMonitoring(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String time = jsonObject.getString("time");
        List<ForecastMonitoring> list = dynamicWarningMapper.queryforecastMonitoring(time);
        List<String> statementList = new ArrayList<>();

        for (ForecastMonitoring forecastMonitoring : list) {
            String time1 = forecastMonitoring.getJkba02a090();  //时间
            String name = forecastMonitoring.getJkba02a030(); //风险区名称
            String fallingWaterTime = forecastMonitoring.getJkba02a050(); //降水时长
            String fallingWaterCount = forecastMonitoring.getJkba02a060(); //降水量
            String waringLevel = forecastMonitoring.getJkba02a070(); // 预警级别
            statementList.add(time1 + ": " + name + " " + " 降水" + fallingWaterTime + "h 降水量" + fallingWaterCount + ",触发" + waringLevel);
        }
        return statementList;
    }


    /**
     * 时间段查询该市（县）累积雨量（降序排序） 以及每个等级的站点个数、最大雨量的站点编号及名称
     *
     * @param jsonObject
     * @return
     */
    @DS("slave_qxdata")
    @Override
    @Transactional
    public PrecipitationInfoByTimeAndNameVo queryPrecipitationInfoByTimeAndName(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String name = jsonObject.getString("name");
        String isCounty = jsonObject.getString("isCounty");
        String cityName = jsonObject.getString("cityName");
        if (name.contains("省")) {
            name = "";
        }
        String cityLevel = jsonObject.getString("cityLevel");
//      市（县）累积雨量（降序排序）
        List<CityCountyCountFallingWaterVo> list1 = dynamicWarningMapper.queryCityCountyCountFallingWater(name, startTime, endTime);
//      每个等级的站点个数
        List<StationNumByLevel> list2 = dynamicWarningMapper.queryStationNumByLevel(name, startTime, endTime);
//      最大雨量的站点编号及名称
        MaxFallWaterAndStationAndId maxFallingWaterCodeAndName = dynamicWarningMapper.queryMaxFallingWaterCodeAndName(name, startTime, endTime);
//      jkba01aList返回
        // 判断是否为区县
        if (!StringUtil.IsNullOrEmpty(isCounty)) {
            name = jsonObject.getString("name");
        }
        List<Qxaa01a> qxaa01aList = dynamicWarningMapper.queryQxaa01a(name, startTime, endTime, isCounty,cityName);
//        if ("sort".equals(name)) {
//            Map<Character, List<Qxaa01a>> groupedByNameInitial = qxaa01aList.stream()
//                    .collect(Collectors.groupingBy(
//                            person -> person.getQxaa01a030(),
//                            Collectors.collectingAndThen(
//                                    Collectors.toList(),
//                                    list -> {
//                                        list.sort(Comparator.comparingDouble(Qxaa01a::getQxaa01a100));
//                                        return list;
//                                    }
//                            )
//                    ));
//        }
        for (Qxaa01a qxaa01a : qxaa01aList) {
            qxaa01a.setQxaa01a070(dmsToLl(String.valueOf(qxaa01a.getQxaa01a070())));
            qxaa01a.setQxaa01a080(dmsToLl(String.valueOf(qxaa01a.getQxaa01a080())));
            qxaa01a.setQxaa01a100(qxaa01a.getQxaa01a100().toString().equals("-99999.0") ? 0.0 : qxaa01a.getQxaa01a100());
        }

        Map<String, Integer> countMap = new HashMap<>();
        Date maxNewDate = new Date(0);
        String maxNewDateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Qxaa01a qxaa01a : qxaa01aList) {
            try {
                Date parse = sdf.parse(qxaa01a.getQxaa01a050());
                maxNewDate = maxNewDate.compareTo(parse) >= 0 ? maxNewDate : parse;
                maxNewDateString = sdf.format(maxNewDate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        List<Qxaa01a> resultList = qxaa01aList.stream()
                .collect(Collectors.toMap(
                        Qxaa01a::getQxaa01a010,
                        q -> q,
                        (q1, q2) -> {
                            double v = (q1.getQxaa01a100().toString().equals("-99999.0") ? 0.0 : q1.getQxaa01a100()) + (q2.getQxaa01a100().toString().equals("-99999.0") ? 0.0 : q2.getQxaa01a100());
                            Double value = Double.parseDouble(Double.toString(v));
                            q1.setQxaa01a100(Double.parseDouble(String.format("%.2f", value)));
                            return q1;
                        }
                ))
                .values()
                .stream()
                .collect(Collectors.toList());

        // 查询最大降水量，以及所在县
        Optional<Qxaa01a> maxList = qxaa01aList.stream().max(Comparator.comparing(Qxaa01a::getQxaa01a100));
        AtomicReference<String> maxName = new AtomicReference<>("");
        AtomicReference<Double> maxValue = new AtomicReference<>(0.0);
        if (maxList != null) {
            maxList.ifPresent(max -> {
//                maxName.set(max.getQxaa01a060());
                maxName.set(max.getQxaa01a020());
                Double maxNum = new BigDecimal(max.getQxaa01a100())
                        .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                maxValue.set(maxNum);
            });
        }

        PrecipitationInfoByTimeAndNameVo precipitationInfoByTimeAndNameVo =
                new PrecipitationInfoByTimeAndNameVo(maxNewDateString, maxName.toString(), maxValue.get(), list1, list2, maxFallingWaterCodeAndName, resultList, resultList);
        return precipitationInfoByTimeAndNameVo;

    }

    /**
     * 度分秒转换经纬度
     *
     * @param dms 度分秒
     */
    public static double dmsToLl(String dms) {
        double ds = Double.parseDouble(dms);
        if (ds > 180) {
            int dd = (int) (ds / 10000);
            int ff = (int) ((ds - dd * 10000) / 100);
            int mm = (int) (ds - dd * 10000 - ff * 100);
            ds = dd + ff / 60.00 + mm / 3600.00;
        }
        return ds;
    }


    /*
     根据时间段、站点编号查询逐小时降水以及逐小时累积
     */
    @DS("slave_qxdata")
    @Override
    public List<TwoIntegetAndTimeVo> queryFallingWaterGrowingByTimeAndStationCode(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String stationCode = jsonObject.getString("stationCode");
        List<IntegetAndTimeVo> list = dynamicWarningMapper.queryFallingWaterGrowingByTimeAndStationCode(startTime, endTime, stationCode);
        boolean allNull = true;
        for (IntegetAndTimeVo element : list) {
            if (element != null) {
                allNull = false;
                break;
            }
        }
        list.forEach(integetAndTimeVo -> {
            //2024-09-23 16:09修改
            //目前数据库中存在雨量为-99999.0的数据，需要过滤掉，先设置为0.0
            if (integetAndTimeVo.getQxaa01a100().toString().equals("-99999.0")) {
                integetAndTimeVo.setQxaa01a100(0.0);
            }
        });
        if (allNull) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<TwoIntegetAndTimeVo> resultList = new ArrayList<>();
        String list0 = sdf.format(list.get(0).getQxaa01a050());
        resultList.add(new TwoIntegetAndTimeVo(list.get(0).getQxaa01a100(), list0, list.get(0).getQxaa01a100()));
        for (int i = 1; i < list.size(); i++) {
            resultList.add(new TwoIntegetAndTimeVo(list.get(i).getQxaa01a100()
                    , sdf.format(list.get(i).getQxaa01a050())
                    , resultList.get(i - 1).getGrowFallingWater() + list.get(i).getQxaa01a100()));
        }
        return resultList;
    }

    @DS("slave_qxdata")
    @Override
    public String queryTimeBysk_time(String ggcf01a020) {
        String time = dynamicWarningMapper.queryTimeBysk_time(ggcf01a020);
        return time;
    }

    @DS("slave_qxdata")
    @Override
    public List<Qxab02b> queryRadarChat(JSONObject jsonObject) {
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        List<Qxab02b> list = dynamicWarningMapper.queryRadarChat(startTime, endTime);
        return list;
    }

    @DS("slave_qxdata")
    @Override
    public List<Qxab03a> queryCloudChat(JSONObject jsonObject) {
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        List<Qxab03a> list = dynamicWarningMapper.queryCloudChat(startTime, endTime);
        return list;
    }

    @DS("slave_qxdata")
    @Override
    public List<Qxbb01a> queryServiceProduct(String wordType, String startTime, String endTime) {
        List<Qxbb01a> list = dynamicWarningMapper.queryServiceProduct(wordType, startTime, endTime);
        return list;
    }

    @DS("master")
    @Override
    /**
     * 查询当天省份的短警告信息
     *
     * @param jsonObject 查询条件的JSON对象
     * @return DynamicWaringShortVo 当天省份的短警告信息
     */
    public DynamicWaringShortVo queryTodayProvinceShortWaring(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String time = jsonObject.getString("time");
        DynamicWaringShortVo dynamicWaringShortVo = dynamicWarningMapper.queryTodayProvinceShortWaring(time);
        return dynamicWaringShortVo;
    }


    @DS("master")
    @Override
    public List<TwoStringVo> queryTodayProvinceMidShortWaring(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String time = jsonObject.getString("time");
        List<TwoStringVo> twoStringVo = dynamicWarningMapper.queryTodayProvinceMidShortWaring(time);
        return twoStringVo;
    }


}


